<?php

/**
 * This is the model class for table "{{family}}".
 *
 * The followings are the available columns in table '{{family}}':
 * @property integer $id
 * @property integer $profile_id
 * @property string $father_name
 * @property integer $father_age
 * @property string $mother_name
 * @property integer $mother_age
 * @property string $father_job
 * @property string $mother_job
 * @property integer $num_of_older_brother
 * @property integer $num_of_older_sister
 * @property integer $num_of_younger_brother
 * @property integer $num_of_younger_sister
 * @property integer $num_of_son
 * @property integer $num_of_daughter
 * @property integer $married
 * @property string $create_date
 * @property integer $created_by
 * @property string $update_date
 * @property integer $updated_by
 *
 * The followings are the available model relations:
 * @property Profile $profile
 */
class Family extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Family the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{family}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('profile_id, married', 'required', 'message' => t ( 'MSG_REQUIRED', 'messages' )),
			array('profile_id, father_age, mother_age, num_of_older_brother, num_of_older_sister, num_of_younger_brother, num_of_younger_sister, num_of_son, num_of_daughter, married', 'numerical', 'integerOnly'=>true, 'message' => t ( 'MSG_WRONG_NUMBER', 'messages' )),
			array('father_name, mother_name, father_job, mother_job', 'length', 'max'=>100, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('create_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, profile_id, father_name, father_age, mother_name, mother_age, father_job, mother_job, num_of_older_brother, num_of_older_sister, num_of_younger_brother, num_of_younger_sister, num_of_son, num_of_daughter, married, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'profile' => array(self::BELONGS_TO, 'Profile', 'profile_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID', 'family'),
			'profile_id' => 'Profile',
			'father_name' => t('LBL_FATHER_NAME', 'family'),
			'father_age' => t('LBL_FATHER_AGE', 'family'),
			'mother_name' => t('LBL_MOTHER_NAME', 'family'),
			'mother_age' => t('LBL_MOTHER_AGE', 'family'),
			'father_job' => t('LBL_FATHER_JOB', 'family'),
			'mother_job' => t('LBL_MOTHER_JOB', 'family'),
			'num_of_older_brother' => t('LBL_OLDER_BROTHER', 'family'),
			'num_of_older_sister' => t('LBL_OLDER_SISTER', 'family'),
			'num_of_younger_brother' => t('LBL_YOUNGER_BROTHER', 'family'),
			'num_of_younger_sister' => t('LBL_YOUNGER_SISTER', 'family'),
			'num_of_son' => t('LBL_SON', 'family'),
			'num_of_daughter' => t('LBL_DAUGHTER', 'family'),
			'married' => t('LBL_MARRIED', 'family'),
			'create_date' => t('LBL_CREATE_DATE', 'family'),
			'created_by' => t('LBL_CREATED_BY', 'family'),
			'update_date' => t('LBL_UPDATE_DATE', 'family'),
			'updated_by' => t('LBL_UPDATED_BY', 'family'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('profile_id',$this->profile_id);
		$criteria->compare('father_name',$this->father_name,true);
		$criteria->compare('father_age',$this->father_age);
		$criteria->compare('mother_name',$this->mother_name,true);
		$criteria->compare('mother_age',$this->mother_age);
		$criteria->compare('father_job',$this->father_job,true);
		$criteria->compare('mother_job',$this->mother_job,true);
		$criteria->compare('num_of_older_brother',$this->num_of_older_brother);
		$criteria->compare('num_of_older_sister',$this->num_of_older_sister);
		$criteria->compare('num_of_younger_brother',$this->num_of_younger_brother);
		$criteria->compare('num_of_younger_sister',$this->num_of_younger_sister);
		$criteria->compare('num_of_son',$this->num_of_son);
		$criteria->compare('num_of_daughter',$this->num_of_daughter);
		$criteria->compare('married',$this->married);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('updated_by',$this->updated_by);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeSave() {
        if (parent::beforeSave()) {

            $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);

            //Get user is logging
            $logedInUser = User::model()->findByPk(Yii::app()->user->id);	   
            
            if ($this->isNewRecord) {
		$this->create_date = $current_date;
		$this->created_by = $logedInUser->username;
	    }
            
            $this->update_date = $current_date;            

            if (isset($logedInUser)) {
                $this->updated_by = $logedInUser->username;               
            }	                            
            
            $this->setIsNewRecord(false);                        
            
            return true;
        } else {
            return false;
        }
    }
        
}